/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import dao.DAOEnseignant;
import dao.DAOEtudiant;
import dao.DAOExamen;
import dao.DAONote;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Cour;
import model.Etudiant;
import model.Examen;
import model.Note;

/**
 *
 * @author Paul
 */
public class DetailMatiere extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            this.doAny(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            this.doAny(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void doAny(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //DAOEnseignant daoEns = new DAOEnseignant();
        //daoEns.
        Etudiant e = (Etudiant) request.getSession().getAttribute("etudiant");
        if (e != null) {
            if (request.getSession().getAttribute("listeExamens") == null && request.getSession().getAttribute("note") == null) {
                DAONote daoNote = new DAONote();
                DAOExamen daoEx = new DAOExamen();
                ArrayList<Cour> listeCours = (ArrayList<Cour>) request.getSession().getAttribute("listeCours");
                ArrayList<Examen> listeExam = daoEx.getExamFromCour(listeCours.get(0).getIdCour());
                request.getSession().setAttribute("listeExamens", listeExam);
                Note note = daoNote.findNoteEtudiantExam(e.getId(), listeExam.get(0).getId());
                request.getSession().setAttribute("note", note);
            }
            request.getRequestDispatcher("WEB-INF/view/consultation/detailMatiere.jsp").forward(request, response);

        } else {
            request.getRequestDispatcher("view/connexion/connexion.jsp").forward(request, response);
        }
    }
}
